package com.ebupt.migu.music.propagate.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebupt.migu.propagate.PropagateStrategy;
import com.ebupt.migu.music.propagate.pojo.dto.StrategyQueryDto;
import org.apache.commons.lang3.StringUtils;

/**
 *  宣发策略
 * @author tz
 */
@DS("mysql_mg")
public interface PropagateStrategyMapper extends BaseMapper<PropagateStrategy> {

    default int createOrUpdate(PropagateStrategy ps){
        return ps.getId() == null ? this.insert(ps) : this.updateById(ps);
    }

    default IPage<PropagateStrategy> listAll(Page<PropagateStrategy> page,StrategyQueryDto dto){
        LambdaQueryWrapper<PropagateStrategy> qry = new LambdaQueryWrapper<>();
        qry.like(StringUtils.isNotEmpty(dto.getStrategyName()),PropagateStrategy::getStrategyName,dto.getStrategyName());
        qry.eq(StringUtils.isNotEmpty(dto.getCreatorName()),PropagateStrategy::getCreatorName,dto.getCreatorName());
        qry.eq(StringUtils.isNotEmpty(dto.getExecuteStatus()),PropagateStrategy::getExecuteStatus,dto.getExecuteStatus());
        qry.eq(StringUtils.isNotEmpty(dto.getCreatorDepartment()),PropagateStrategy::getCreatorDepartment,dto.getCreatorDepartment());
        qry.between(StringUtils.isNotEmpty(dto.getCreateTimeStart()) && StringUtils.isNotEmpty(dto.getCreateTimeEnd())
                ,PropagateStrategy::getCreateTime,dto.getCreateTimeStart(),dto.getCreateTimeEnd());
        qry.le(StringUtils.isNotEmpty(dto.getStartTime()),PropagateStrategy::getStartTime,dto.getStartTime());
        qry.ge(StringUtils.isNotEmpty(dto.getEndTime()),PropagateStrategy::getEndTime,dto.getEndTime());
        qry.ne(PropagateStrategy::getExecuteStatus,7);
        qry.orderByDesc(PropagateStrategy::getCreateTime);
        return this.selectPage(page,qry);
    }

}
